//
//  MainViewController.swift
//  微博项目
//
//  Created by zhaoyinpeng on 16/3/6.
//  Copyright © 2016年 zhaoyinpeng. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    
    @objc private func plusBtnDidClick() {
        print(__FUNCTION__)
        
        let compose = ComposeViewController()
        let nav = UINavigationController(rootViewController: compose)
        //.modal出 发布微博控制器
        presentViewController(nav, animated: true, completion: nil)
        
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
   
//        print(tabBar.classForCoder)
        //.由于系统的tabbar是只读属性，不能够直接设置，通过kvc的方式在运行时 替换身份
        let mainTab = mainTabbar()
        setValue(mainTab, forKey: "tabBar")
        
//        print(tabBar.classForCoder)
        
        mainTab.plusBtn.addTarget(self, action: "plusBtnDidClick", forControlEvents: .TouchUpInside)
        
        
        //.添加所有子视图控制器
        addChildViewControllers()
        
        
        
    }
    
    
    private func addChildViewControllers() {
        
       addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
        
    }
    
    
    //.MARK: 添加子视图控制器
    private func addChildViewController(vc: UIViewController,title: String,imageName: String) {
    
        
        //.设置title
        vc.tabBarItem.title = title
        
         //.设置tintColor
        tabBar.tintColor = UIColor.orangeColor()
        //.设置图片
        vc.tabBarItem.image = UIImage(named:imageName)
        
        let nav = UINavigationController(rootViewController: vc)
        
        
        //.设置选中图片
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        
        //.设置字体
        vc.tabBarItem.titleTextAttributesForState(.Normal)
        
        
        vc.navigationItem.title = title
        
        
        addChildViewController(nav)
        
        
    }
    

   
}
